package com.kx.base.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring上下文的工具类
 * @Author Sun
 * @Date 2021/7/26 21:39
 */
@Component
public class SpringContentUtil implements ApplicationContextAware {

    /**
     * 获取应用程序上下文对象
     * 获取Ioc容器中的对象的时候，要根据上下文对象进行获取
     */
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContentUtil.applicationContext = applicationContext;
    }

    /**
     * 调用Spring容器上下文获取对应类型的对象
     */
    public static <T> T getBean(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }
}
